#define _XOPEN_SOURCE
#include <func.h>
int isUsrLegal(const char *usr_name, const char *pwd);
void get_salt(char *salt, char *pwd);
int isUsrLegal(const char *usr_name, const char *pwd){
    struct spwd *sp;
    sp = getspnam(usr_name);
    if(sp == NULL)
        return -1;
    char salt[100] = {0};
    get_salt(salt, sp->sp_pwdp);
    if(strcmp(sp->sp_pwdp, crypt(pwd, salt)) == 0) 
        return 0;
    else
        return -1;
}

void get_salt(char *salt, char *pwd){
    int i, j;
    for(i = 0, j = 0; pwd[i] && j != 3; ++i){
        if(pwd[i] == '$')
            ++j;
    }
    strncpy(salt, pwd, i - 1);
}

